<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<%
  add_crumb @quiz.title, context_url(@context, :context_quiz_url, @quiz)
  add_crumb t(:page_title, 'Moderate Quiz')
%>
<% content_for :page_title do %><%= t(:moderate_page_title, "%{title}: Moderate", :title => @quiz.title) %><% end %>
<div class="alert" id="outstanding_quiz_submissions_found" style="display: none">
  {{#t}}There are outstanding quiz submissions.{{/t}}
  <a href="#" role="button" class="icon-not-graded outstanding_link" id="check_outstanding">
    {{#t}}Check on outstanding quiz submissions{{/t}}
  </a>
</div>
<div class="moderate_header">
  <h1>{{#t}}Moderate Quiz{{/t}}</h1>
</div>
<%= form_tag(context_url(@context, :context_quiz_moderate_url, @quiz.id), method: :get) do %>
  <div class="ic-Form-control">
    <div class="ic-Input-group">
      <%= text_field_tag :search_term, params[:search_term], class: "ic-Input", placeholder: t('Search People'),
      aria_label: t('Search people. As you type in this field, the list of people will be automatically filtered to only include those whose names match your input.') %>
      <%= submit_tag t('Filter'), class: "Button" %>
    </div>
  </div>
<% end %>
<table id="students" class="ic-Table ic-Table--striped <%= 'can_add_attempts' if @quiz.allowed_attempts < 0 %>">
  <thead>
    <tr>
      <th scope="col" class="check">
        <input type="checkbox" id="check_all" title="{{#t}}Select/Deselect All{{/t}}"/>
      </th>
      <th scope="col" class="name">{{#t}}Student{{/t}}</th>
      <th scope="col">{{#t}}Attempt{{/t}}</th>
      <th scope="col">{{#t}}Time{{/t}}</th>
      <th scope="col" style="<%= hidden if @quiz.allowed_attempts < 0 %>">{{#t}}Attempts Left{{/t}}</th>
      <th scope='col'>{{#t}}Score{{/t}}</th>
      <% manually_unlocked_title = t('titles.manually_unlocked', "Manually Unlocked") %>
      <th scope="col" class="unlocked_col">&nbsp;</th>
      <th scope="col" class="edit">
        <a href="#" class="reload_link" role="button"><%= image_tag "ajax-reload.gif", alt: t(:reload_student_table, "Reload student table") %></a>
      </th>
    </tr>
  </thead>
  <tbody>
    <% count = @students.offset %>
    <% submissions_by_student = @submissions.index_by(&:user_id) %>
    <% @students.each do |student| %>
      <% count += 1 %>
      <% submission = submissions_by_student[student.id] %>
      <% student_name = (@quiz.survey? && @quiz.anonymous_submissions) ? t(:student_n, "Student %{n}", :n => count) : student.last_name_first %>
      <tr class="student <%= 'extendable' if submission && submission.extendable? %> <%= 'in_progress' if submission && submission.untaken? %> <%= 'manually_unlocked' if submission && submission.manually_unlocked %>" id="student_<%= student.id %>" data-started-at="<%= submission && submission.started_at && submission.started_at.iso8601 %>" data-end-at="<%= submission && submission.end_at && submission.end_at.iso8601 %>" data-user-id="<%= student.id %>">
        <td class="check">
          <input type="checkbox" class="student_check" data-id="<%= student.id %>" title="<%= t('titles.select_user', "Select %{user}", :user => student_name) %>"/>
        </td>
        <td class="name">
          <%= t(:student_gets_extra_time,
              {one: "*%{student_name}* **gets 1 extra minute on each attempt**",
                other:"*%{student_name}* **gets %{count} extra minutes on each attempt**" },
              {
               student_name: student_name,
               count: (submission && submission.extra_time) || '0',
               wrapper: {
                 '*' => submission ? %{<a class="student_name" href="#{context_url(@context, :context_quiz_history_url, @quiz.id, :quiz_submission_id => submission.id)}">\\1</a>} : '\1',
                 '**' => %{<div class='extra_time_allowed' style="#{hidden unless submission && submission.extra_time && submission.extra_time != "0"}">\\1</div>}
               },
             })
          %>
        </td>
        <td class="attempt number"><%= submission && !submission.settings_only? ? submission.attempt : "--" %></td>
        <td class="time_holder">
          <span class="time">
          <% if submission && !submission.settings_only? %>
            <% if submission.completed? %>
              <%= t(:finished_in, "finished in %{duration}", :duration => submission.finished_in_words) %>
            <% elsif submission.overdue? %>
              --
            <% else %>
              {{#t}}in progress{{/t}}
            <% end %>
          <% end %>
          </span>
          <a href="#" class="extend_time_link no-hover"><%= image_tag "history.png", :alt => t(:add_time, "Add Time") %></a>
        </td>
        <td class="attempts_left number" style="<%= hidden if @quiz.allowed_attempts < 0 %>">
          <% if @quiz.allowed_attempts < 0 %>
            --
          <% elsif submission %>
            <%= submission.attempts_left %>
          <% else %>
            <%= @quiz.allowed_attempts %>
          <% end %>
        </td>
        <td class="score_holder">
          <span class="score"><%= submission && submission.kept_score %></span>
        </td>
        <td class="unlocked_col">
          <span class="unlocked" title="<%= manually_unlocked_title %>" style="<%= hidden unless submission && submission.manually_unlocked %>"><i class="icon-unlock standalone-icon" alt="<%= manually_unlocked_title %>">
        </td>
        <td class="edit links">
          <% cue_title = t('titles.change_user_extensions', "Change user extensions") %>
          <span class="extra_attempts" style="display: none;"><%= submission && submission.extra_attempts %></span>
          <a href="#" class="moderate_student_link no-hover" title="<%= cue_title %>"><i class="icon-edit standalone-icon"></i></a>
        </td>
      </tr>
    <% end %>
  </tbody>
</table>
<%= will_paginate(@students) %>

<a href="#" class="icon-edit moderate_multiple_link" role="button" style="display: none;">
  <%= t('buttons.change_extensions_for_selected_students', "Change Extensions for %{n} Selected Students", :n => raw('<span id="checked_count">X</span>')) %>
</a>

<div id="extend_time_dialog" class="student_extension_dialog" style="display: none;">
  <p>
    {{#t}}You can give this student extra time on their current quiz attempt. How much time would you like to give them?{{/t}}
  </p>

  <dl class="clearfix">
    <dt><%= before_label(:started, "Started") %></dt>
    <dd class="started_at"></dd>
    <dt><%= before_label(:ending, "Ending") %></dt>
    <dd class="end_at"></dd>
  </dl>

  <div class="relative_time">
    <label for="extend_quiz_mins">
      {{#t}}End the quiz{{/t}}
    </label>

    <input type="text" name="time" title="{{#t}}minutes to add{{/t}}" />

    <label for="extend_from_time">
      {{#t}}minutes from{{/t}}
    </label>

    <select name="time_type">
      <option value="extend_from_now">{{#t}}now{{/t}}</option>
      <option value="extend_from_end_at">{{#t}}the current end time{{/t}}</option>
    </select>

    <p class="note">
      {{#t}}Maximum of 1440 minutes (24 hours){{/t}}
    </p>
  </div>

  <div class="button-container">
    <button type="button" class="btn cancel_button">{{#t}}Cancel{{/t}}</button>
    <button type="button" class="btn btn-primary save_button">{{#t}}Extend Time{{/t}}</button>
  </div>
</div>

<div id="moderate_student_dialog" class="student_extension_dialog" style="display: none;">
  <h2>{{#t}}Student Extensions{{/t}}</h2>

  <%= form_for :quiz, :url => "", :html => {:id => "moderate_student_form", :method => :post} do |f| %>

    <% if @quiz.allowed_attempts > 0 %>
      <div class="field extra_attempts">
        <label for="extension_extra_attempts">
          <%= before_label(:extra_attempts, "Extra Attempts") %>
          <em class="note"><%= t(:global_extra_attempts, "everyone already gets %{n}", :n => @quiz.allowed_attempts) %></em>
        </label>
        <span class="input">
          <input id="extension_extra_attempts" type="text" name="extra_attempts" title="{{#t}}additional extra attempts{{/t}}"/>
          <span class="units">{{#t}}attempts{{/t}}</span>
        </span>
      </div>
    <% end %>

    <% if @quiz.time_limit %>
      <div class="field extra_time">
        <label for="extra_time">
          {{#t}}Extra time on every attempt:{{/t}}
          <em class="note"><%= t(:global_extra_attempt_time, "everyone already gets %{count} minutes", :count => @quiz.time_limit) %></em>
        </label>
        <span class="input">
          <input id="extension_extra_time" type="text" name="extra_time" title="{{#t}}additional minutes on each attempt{{/t}}"/>
          <span class="units">{{#t}}minutes{{/t}}</span>
        </span>
      </div>
    <% end %>

    <% if @quiz.one_time_results %>
      <div class="field one_time_results">
        <input type="hidden" name="reset_has_seen_results" value="0"/>
        <input type="checkbox" name="reset_has_seen_results" id="extension_reset_has_seen_results" value="1" />
        <label for="extension_reset_has_seen_results">
          <%= t('labels.reset_has_seen_results',
          "Let the student see the results one more time") %>
        </label>
      </div>
    <% end %>

    <div class="field manually_unlock">
      <input type="hidden" aria-hidden='true' name="manually_unlocked" value="0"/>
      <input type="checkbox" name="manually_unlocked" id="extension_manually_unlocked" value="1"/>
      <label for="extension_manually_unlocked">
        {{#t}}Manually unlock the quiz for the next attempt{{/t}}
      </label>
    </div>

    <div class="button-container">
      <button type="button" class="btn dialog_closer cancel_button">{{#t}}Cancel{{/t}}</button>
      <button type="submit" class="btn btn-primary save_button">{{#t}}Save{{/t}}</button>
    </div>
  <% end %>
</div>

<div class="student-extension-dialog" id="autosubmit_form" tabindex="0" style="display: none;">
  <div id="autosubmit_content_description_things_to_do" style="display: none">
    {{#t}}These quiz submissions are outstanding, likely because a student left the quiz page prior to submission.  If you would like to grade them, please press 'Submit' below. {{/t}}
  </div>

  <div id="autosubmit_content" class="autosubmit_form_dialog_content with-scrolling-right-side">
    <div class='child_container'>
      <dl class="outstanding_submissions_list">
        <dt class="example_autosubmit_row autosubmit_data_row" style="display: none;">
          <input type="checkbox" name="quiz_submission_ids[]" class="autosubmit_selection_checkbox" value=""/>
          <label for="student_selection"><%= "student_name" %></label>
        </dt>
      </dl>
    </div>
  </div>

  <div class="button-container autosubmit_form_controls">
    <button type="button" id="autosubmit_form_cancel_btn" class="btn dialog_closer cancel_button">{{#t}}Cancel{{/t}}</button>
    <button id="autosubmit_form_submit_btn" class="btn btn-primary submit-btn" data-text-while-loading='{{#t}}Submitting...") %>' type="submit" disabled><%= t('#buttons.submit', "Submit{{/t}}</button>
  </div>
</div>

<div style="display: none;">
  <span class="now"><%= ActiveSupport::TimeWithZone.new(Time.now.utc, Time.zone) %></span>
  <a href="<%= context_url(@context, :context_quiz_moderate_url, @quiz.id, :page => params[:page], :last_updated_at => "{{ update }}") %>" class="update_url">&nbsp;</span>
  <% last_updated_at = @submissions.empty? ? nil : (@submissions.map(&:updated_at).max - 5.minutes).in_time_zone.iso8601 %>
  <span class="last_updated_at"><%= last_updated_at %></span>
  <a href="<%= context_url(@context, :context_quiz_extensions_url, @quiz.id, "{{ user_id }}") %>" class="extension_url">&nbsp;</a>
  <a href="<%= context_url(@context, :api_v1_outstanding_quiz_submission_index_url, @quiz) %>" class="outstanding_index_url">&nbsp;</a>
  <a href="<%= context_url(@context, :api_v1_outstanding_quiz_submission_grade_url, @quiz) %>" class="outstanding_grade_url">&nbsp;</a>
</div>

<% js_bundle :moderate_quiz %>
<% css_bundle :moderate_quiz %>
